package org.kev.transit.worker;

import org.kev.transit.TransitMapActivity;

import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Handler;
import android.util.Log;

import com.google.android.maps.Point;

public class MapWorker implements Runnable {
	private static final String TAG = "MapWorker";
	private TransitMapActivity parent = null;
	private Runnable mCallback=null;
	private Handler mHandler=null;
	
	public MapWorker(TransitMapActivity act,Runnable callback, Handler handler){
		parent = act;
		mCallback=callback;
		mHandler=handler;
		
	}
	public void run(){
		LocationManager locManager= (LocationManager) parent.getSystemService(Context.LOCATION_SERVICE);
		/*
		 * List providers = locManager.getProviders(); for (int i=0;i<providers.size();i++){
		 * Log.d(TransitConst.TAG,"prov="+providers.get(i) ); }
		 * Log.d(TransitConst.TAG,"prov="+providers);
		 */
		
		Location currentLoc = locManager.getCurrentLocation("gps");
		if(currentLoc==null){
			currentLoc = locManager.getCurrentLocation("cell");
			Log.e(TAG,"null currentLoc from currentLocation");
			
			mHandler.post(mCallback);
			return;
		}
		int lati = parent.getMLat(currentLoc.getLatitude());
		int longi = parent.getMLong(currentLoc.getLongitude());
		Log.d(TAG,"curr gps lat="+lati+" lon="+longi);
		Point p = new Point(lati, longi);
		parent.overlay = new LocOverlay(parent.stationGPS, p);
		Log.d(TAG, "loc overlay = " + parent.overlay);
		parent.overlayControl.add(parent.overlay, true);
		
		mHandler.post(mCallback);
		//parent.mapLocation();
		
	}

}
